//
//  ExpenseIncomeController.m
//  MoneyIndividual
//
//  Created by Le Anh Tai on 12/24/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "ExpenseIncomeController.h"
#import "CalloutView.h"

@interface ExpenseIncomeController ()

@end

@implementation ExpenseIncomeController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _bottomView = [[UIView alloc] init];
    _bottomView.layer.borderColor = COLOR_GRAY_LIGHT.CGColor;
    _bottomView.layer.borderWidth = 1;
    _bottomView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_bottomView];
    
    _chiButton = [self newButtonToolbarWithTitle:NSLocalizedString(@"CHI", nil) selectedBackgroundColor:COLOR_RED];
    
    _thuButton = [self newButtonToolbarWithTitle:NSLocalizedString(@"THU", nil) selectedBackgroundColor:COLOR_GREEN];
    
    _expenseController = [[ExpenseController alloc] init];
    [self.view addSubview:_expenseController.view];
    
    _incomeController = [[IncomeController alloc] init];
    [self.view addSubview:_incomeController.view];
    
    [self performSelector:@selector(buttonPress:) withObject:_chiButton afterDelay:0.2f];
}

- (UIButton*)newButtonToolbarWithTitle:(NSString*)title selectedBackgroundColor:(UIColor*)backgroundColor
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitleColor:COLOR_BLACK_PURE forState:UIControlStateNormal];
    [button setTitleColor:COLOR_WHITE_PURE forState:UIControlStateHighlighted];
    [button setTitleColor:COLOR_WHITE_PURE forState:UIControlStateSelected];
    [button setTitle:title forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor clearColor]];
    [button setBackgroundImage:[UIImage imageFromColor:COLOR_WHITE_PURE] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageFromColor:backgroundColor] forState:UIControlStateHighlighted];
    [button setBackgroundImage:[UIImage imageFromColor:backgroundColor] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:button];
    return button;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    _bottomView.frame = RECT(0, self.view.height - HEI_HEADER_VIEW, self.view.width, HEI_HEADER_VIEW);
    
    _thuButton.frame = RECT(0, 0, self.view.width/2, _bottomView.height);
    _chiButton.frame = RECT(_thuButton.right, 0, _thuButton.width, _thuButton.height);
    
    _expenseController.view.frame = RECT(0, 0, self.view.width, self.view.height - _bottomView.height);
    [_expenseController viewWillLayoutSubviews];
    
    _incomeController.view.frame = _expenseController.view.frame;
    [_incomeController viewWillLayoutSubviews];
}

- (void)selectExpenseTab
{
    _chiButton.selected = YES;
    _thuButton.selected = NO;
    _expenseController.view.hidden = YES;
    _incomeController.view.hidden = NO;
}

- (void)selectIncomeTab
{
    _chiButton.selected = NO;
    _thuButton.selected = YES;
    _expenseController.view.hidden = NO;
    _incomeController.view.hidden = YES;
}

#pragma mark - Button delegate
- (void)buttonPress:(UIButton*)sender
{
    if ([sender isEqual:_thuButton])
    {
        [self selectIncomeTab];
    }
    else
    {
        [self selectExpenseTab];
    }
}
@end
